package com.ph.irp.stock.service.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.ph.exception.PHException;
import com.ph.irp.base.measure.dao.PBaseMeasureUnitDAO;
import com.ph.irp.base.verifyproject.dao.PBaseVerifyprogectSupplierDao;
import com.ph.irp.base.verifyproject.dao.PBaseVerifyprojectCandiateDao;
import com.ph.irp.base.verifyproject.dao.PBaseVerifyprojectDepartmentDao;
import com.ph.irp.base.verifyproject.dao.PBaseVerifyprojectMaterialDao;
import com.ph.irp.base.verifyproject.dao.PBaseVerifyprojectStorageDao;
import com.ph.irp.dto.StockInStorageDTO;
import com.ph.irp.dto.StockInStorageDetailDTO;
import com.ph.irp.model.PStockInStorage;
import com.ph.irp.model.PStockInStorageDetail;
import com.ph.irp.stock.dao.StockInStorageDAO;
import com.ph.irp.stock.dao.StockInStorageDetailDAO;
import com.ph.irp.stock.service.StockInStorageService;

public class StockInStorageServiceImpl implements StockInStorageService {

	/**
	 * 
	 */
	private static final long serialVersionUID = 386334669936096188L;
	private StockInStorageDAO stockInStorageDAOProxy;
	private StockInStorageDetailDAO stockInStorageDetailDAOProxy;
	private PBaseVerifyprogectSupplierDao supplierDaoProxy;
	private PBaseVerifyprojectDepartmentDao departmentDaoProxy;
	private PBaseVerifyprojectCandiateDao candiateDaoProxy;
	private PBaseVerifyprojectMaterialDao materialDaoProxy;
	private PBaseVerifyprojectStorageDao storageDaoProxy;
	private PBaseMeasureUnitDAO measureUnitDaoProxy;

	public PBaseMeasureUnitDAO getMeasureUnitDaoProxy() {
		return measureUnitDaoProxy;
	}

	public void setMeasureUnitDaoProxy(PBaseMeasureUnitDAO measureUnitDaoProxy) {
		this.measureUnitDaoProxy = measureUnitDaoProxy;
	}

	public PBaseVerifyprojectStorageDao getStorageDaoProxy() {
		return storageDaoProxy;
	}

	public void setStorageDaoProxy(PBaseVerifyprojectStorageDao storageDaoProxy) {
		this.storageDaoProxy = storageDaoProxy;
	}

	public PBaseVerifyprojectMaterialDao getMaterialDaoProxy() {
		return materialDaoProxy;
	}

	public void setMaterialDaoProxy(PBaseVerifyprojectMaterialDao materialDaoProxy) {
		this.materialDaoProxy = materialDaoProxy;
	}

	public StockInStorageDAO getStockInStorageDAOProxy() {
		return stockInStorageDAOProxy;
	}

	public void setStockInStorageDAOProxy(
			StockInStorageDAO stockInStorageDAOProxy) {
		this.stockInStorageDAOProxy = stockInStorageDAOProxy;
	}

	public StockInStorageDetailDAO getStockInStorageDetailDAOProxy() {
		return stockInStorageDetailDAOProxy;
	}

	public void setStockInStorageDetailDAOProxy(
			StockInStorageDetailDAO stockInStorageDetailDAOProxy) {
		this.stockInStorageDetailDAOProxy = stockInStorageDetailDAOProxy;
	}

	public PBaseVerifyprogectSupplierDao getSupplierDaoProxy() {
		return supplierDaoProxy;
	}

	public void setSupplierDaoProxy(
			PBaseVerifyprogectSupplierDao supplierDaoProxy) {
		this.supplierDaoProxy = supplierDaoProxy;
	}

	public PBaseVerifyprojectDepartmentDao getDepartmentDaoProxy() {
		return departmentDaoProxy;
	}

	public void setDepartmentDaoProxy(
			PBaseVerifyprojectDepartmentDao departmentDaoProxy) {
		this.departmentDaoProxy = departmentDaoProxy;
	}

	public PBaseVerifyprojectCandiateDao getCandiateDaoProxy() {
		return candiateDaoProxy;
	}

	public void setCandiateDaoProxy(
			PBaseVerifyprojectCandiateDao candiateDaoProxy) {
		this.candiateDaoProxy = candiateDaoProxy;
	}

	@Override
	public void addStockInStorage(PStockInStorage stockInStorage)
			throws Throwable {
		stockInStorageDAOProxy.addStockInStorage(stockInStorage);
	}

	@Override
	public void addStockInStorageDetail(
			PStockInStorageDetail stockInStorageDetail) throws Throwable {
		stockInStorageDetailDAOProxy
				.addStockInStorageDetail(stockInStorageDetail);

	}

	@Override
	public void deleteStockInStorage(String stockInStorageId) throws Throwable {
		PStockInStorage theStockInStorage = stockInStorageDAOProxy.getStockInStorageById(Integer.parseInt(stockInStorageId));
		if(theStockInStorage.getIsAuditing() == 1) {
			throw new PHException("不能删除已审核的单据");
		}
		if(theStockInStorage.getIsHook() == 1) {
			throw new PHException("不能删除已勾稽的单据");
		}
		stockInStorageDetailDAOProxy.deleteStockInStorageDetailByInStorageId(stockInStorageId);
		stockInStorageDAOProxy.deleteStockInStorage(stockInStorageId);
		
	}

	@Override
	public int getStockInStorageIdByCode(String stockInStorageCode)
			throws Throwable {
		return stockInStorageDAOProxy
				.getStockInStorageIdByCode(stockInStorageCode);
	}

	@Override
	public List<StockInStorageDTO> getAllStockInStorages() throws Throwable {
		return convertSInStorageListTODTOList(stockInStorageDAOProxy
				.getAllStockInStorages());
	}

	@Override
	public List<StockInStorageDTO> getStockInStorages(PStockInStorage stockInStorage, String beginDate, String endDate) throws Throwable {
		return convertSInStorageListTODTOList(stockInStorageDAOProxy.getStockInStorages(stockInStorage, beginDate, endDate));
	}

	public List<StockInStorageDTO> convertSInStorageListTODTOList(
			List<PStockInStorage> stockInStorages) throws Throwable {
		List<StockInStorageDTO> stockInStorageDTOs = new ArrayList<StockInStorageDTO>();
		Iterator<PStockInStorage> iterator = stockInStorages.iterator();
		StockInStorageDTO stockInStorageDTO = null;
		PStockInStorage stockInStorage = null;
		while (iterator.hasNext()) {
			stockInStorageDTO = new StockInStorageDTO();
			stockInStorage = iterator.next();
			stockInStorageDTO.setStockInStorage(stockInStorage);
			stockInStorageDTO.setSupplier(supplierDaoProxy
					.getSupplierById(stockInStorage.getSupplierId()));
			stockInStorageDTO.setDepartment(departmentDaoProxy
					.getDepartmentById(stockInStorage.getDepartmentId()));
			stockInStorageDTO.setProtector(candiateDaoProxy
					.getCandiateById(stockInStorage.getProtectorId()));
			stockInStorageDTO.setManager(candiateDaoProxy
					.getCandiateById(stockInStorage.getManagerId()));
			stockInStorageDTO.setAcceptancer(candiateDaoProxy
					.getCandiateById(stockInStorage.getAcceptancerId()));
			stockInStorageDTO.setBusinesser(candiateDaoProxy
					.getCandiateById(stockInStorage.getBusinesserId()));
			stockInStorageDTOs.add(stockInStorageDTO);

		}
		return stockInStorageDTOs;
	}

	@Override
	public List<StockInStorageDetailDTO> getStockInStorageDetailsByInStorageId(int stockInStorageId)
			throws Throwable {
		return convertDetailModelListToDTOList(stockInStorageDetailDAOProxy.getStockInStorageDetailsByStockInStorageId(stockInStorageId));
	}
	
	public List<StockInStorageDetailDTO> convertDetailModelListToDTOList(List<PStockInStorageDetail> stockInStorageDetails) throws Throwable {
		List<StockInStorageDetailDTO> stockInStorageDetailDTOs = new ArrayList<StockInStorageDetailDTO>();
		StockInStorageDetailDTO stockInStorageDetailDTO = null;
		Iterator<PStockInStorageDetail> iterator = stockInStorageDetails.iterator();
		PStockInStorageDetail stockInStorageDetail = null;
		while(iterator.hasNext()) {
			stockInStorageDetailDTO = new StockInStorageDetailDTO();
			stockInStorageDetail = iterator.next();
			stockInStorageDetailDTO.setStockInStorageDetail(stockInStorageDetailDAOProxy.getStockInStorageDetailById(stockInStorageDetail.getStockInStorageDetailId()));
			stockInStorageDetailDTO.setStockInStorage(stockInStorageDAOProxy.getStockInStorageById(stockInStorageDetail.getStockInStorageId()));
			stockInStorageDetailDTO.setMaterial(materialDaoProxy.getMaterialById(stockInStorageDetail.getMaterialId()));
			stockInStorageDetailDTO.setStorage(storageDaoProxy.getStorageById(stockInStorageDetail.getStorageId()));
			stockInStorageDetailDTO.setMeasureUnit(measureUnitDaoProxy.getMeasureUnitById(stockInStorageDetail.getMeasureUnitId()));
			stockInStorageDetailDTOs.add(stockInStorageDetailDTO);
		}
		return stockInStorageDetailDTOs;
	}

	@Override
	public StockInStorageDTO getStockInStorageById(int stockInStorageId)
			throws Throwable {
		List<PStockInStorage> tempList = new ArrayList<PStockInStorage>();
		tempList.add(stockInStorageDAOProxy.getStockInStorageById(stockInStorageId));
		return convertSInStorageListTODTOList(tempList).get(0);
	}
 
}
